<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:c="http://java.sun.com/jsp/jstl/core"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:p="http://primefaces.prime.com.tr/ui"
                template="./../template/WorkspaceTemplate.xhtml">

    <ui:define name="top">

    </ui:define>

    <ui:define name="left">

    </ui:define>

    <ui:define name="right">

    </ui:define>

    <ui:define name="content">
        <style>
            .ui-state-highlight.ui-selected {
                color: blueviolet;
            }
        </style>
        <h2>Edit Sales Quotation</h2>
        <h:form id="salesQuotationForm" style="font-size: 85%">

            <p:panel header="Sales Quotation" style="font-size: 110%">
                <p:messages />
                <br/>
                <br/>

                <h:outputText value="Order Items" style="font-weight: bold; font-size: 110%"/>
                <p:separator title="Order Items"/> 
                <br/>
                <p:dataTable var="oneIL" value="#{sqManagerBean.salesQuotation.itemLines}">
                    <p:column headerText="Product Type" rendered="#{oneIL.qtyOrderedCase != 0}">
                        <h:outputText value="#{oneIL.product.productType}" />
                    </p:column>
                    <p:column headerText="Quantity Ordered Case" rendered="#{oneIL.qtyOrderedCase != 0}">
                        <h:outputText value="#{oneIL.qtyOrderedCase}" />
                    </p:column>
                    <p:column headerText="Unit Price (SGD)" rendered="#{(oneIL.qtyOrderedCase != 0) &amp;&amp; (sqManagerBean.salesQuotation.currency)}">
                        <h:outputText value="#{oneIL.unitPriceSGD}" >
                            <f:convertNumber type="currency"/>
                        </h:outputText>
                    </p:column>
                    <p:column headerText="Unit Price (USD)" rendered="#{(oneIL.qtyOrderedCase != 0) &amp;&amp; (!sqManagerBean.salesQuotation.currency)}">
                        <h:outputText value="#{oneIL.unitPriceUSD}">
                            <f:convertNumber type="currency"/>
                        </h:outputText>
                    </p:column>
                    <p:column headerText="Member Discount" rendered="#{oneIL.qtyOrderedCase != 0}">
                        <h:outputText value="#{oneIL.memberDiscount}" />
                    </p:column>
                    <p:column headerText="Order Discount" rendered="#{oneIL.qtyOrderedCase != 0}">
                        <h:outputText value="#{oneIL.orderDiscount}" />
                    </p:column>
                    <p:column headerText="Special Discount" rendered="#{oneIL.qtyOrderedCase != 0}">
                        <h:inputText value="#{oneIL.specialDiscount}" />
                    </p:column>
                </p:dataTable>
                <br/>
                <br/>
                <br/>

                <h:outputText value="Shipping Terms" style="font-weight: bold; font-size: 110%"/>
                <p:separator title="Shipping Terms"/>
                <h:panelGrid id="panelgrid2" columns="6" cellpadding="10">
                    <h:outputLabel style="font-weight: bold" value="Freight On Board: "/>
                    <h:selectBooleanCheckbox id="fob" value="#{sqManagerBean.salesQuotation.freightOnBoard}"/>
                    <h:outputLabel style="font-weight: bold" value=""/>
                    <h:outputLabel style="font-weight: bold" value=""/>
                    <h:outputLabel style="font-weight: bold" value=""/>
                    <h:outputLabel style="font-weight: bold" value=""/>
                    <h:outputLabel style="font-weight: bold" value="Shipping Details: "/>
                    <h:selectOneRadio id="shipment" value="#{sqManagerBean.salesQuotation.fullShipment}">
                        <f:selectItem itemLabel="Full Shipment" itemValue="true"/>
                        <f:selectItem itemLabel="Partial Shipment" itemValue="false"/>
                        <p:ajax listener="#{sqManagerBean.setDeliveryOrder}" update="shipmentDetails"/>
                    </h:selectOneRadio>
                </h:panelGrid>

                <br/>
                <h:panelGroup id="shipmentDetails">
                    <br/>
                    <br/>
                    <br/>

                    <h:outputText value="Delivery Order" style="font-weight: bold; font-size: 110%"/>
                    <h:panelGroup rendered="#{sqManagerBean.salesQuotation.fullShipment}" >
                        <p:separator/>
                        <br/>
                        <h:panelGrid columns="2">
                            <h:outputLabel value="Urgent: "/>
                            <h:selectBooleanCheckbox value="#{sqManagerBean.newDeliveryOrder.urgent}">
                                <p:ajax update="salesQuotationForm"/>
                            </h:selectBooleanCheckbox>
                            <h:panelGroup rendered="#{sqManagerBean.newDeliveryOrder.urgent}">
                                <h:outputLabel value="Requested Lead Time: " />
                            </h:panelGroup>
                            <h:panelGroup rendered="#{sqManagerBean.newDeliveryOrder.urgent}">
                                <p:calendar value="#{sqManagerBean.date1}" onSelectUpdate="salesQuotationForm" showOn="button" />
                            </h:panelGroup>
                        </h:panelGrid>
                        <br/>
                        <h:panelGrid id="sa1" columns="2">
                            <h:outputLabel value="Address Line 1: "/>
                            <p:inputText disabled="true" size="80" value="#{sqManagerBean.newDeliveryOrder.shippingAddress.addressLine1}" label="Shipping Address Line 1" required="true" requiredMessage="Delivery Order Shipping Address Line 1 required"/>

                            <h:outputLabel value="Address Line 2: "/>
                            <p:inputText disabled="true" size="80" value="#{sqManagerBean.newDeliveryOrder.shippingAddress.addressLine2}" label="Shipping Address Line 2" required="true" requiredMessage="Delivery Order Shipping Address Line 2 required"/>

                            <h:outputLabel value="Zip Code: "/>
                            <p:inputMask disabled="true" mask="999999" value="#{sqManagerBean.newDeliveryOrder.shippingAddress.zipCode}" label="Zip Code" required="true" requiredMessage="Delivery Order Shipping Address Zip Code required"/>

                            <h:outputLabel value="City: "/>  
                            <p:inputText disabled="true" size="20" value="#{sqManagerBean.newDeliveryOrder.shippingAddress.city}" label="City" required="true" requiredMessage="Delivery Order Shipping Address City required"/>

                            <h:outputLabel value="State: "/>
                            <p:inputText disabled="true" size="20" value="#{sqManagerBean.newDeliveryOrder.shippingAddress.stateName}" label="State" required="true" requiredMessage="Delivery Order Shipping Address State required"/>

                            <h:outputLabel value="Country: "/>
                            <p:inputText disabled="true" size="20" value="#{sqManagerBean.newDeliveryOrder.shippingAddress.country}" label="Country" required="true" requiredMessage="Delivery Order Shipping Address Country required"/>
                        </h:panelGrid>
                        <br/>
                        <h:panelGrid columns="3" cellpadding="10">
                            <h:outputLabel value="Indicative Lead Time: "/>
                            <h:outputText value="#{sqManagerBean.newDeliveryOrder.indicativeLeadTime}"/>
                            <p:commandButton value="Available to promise check" actionListener="#{sqManagerBean.aTPCheck1}" update="salesQuotationForm"/>
                        </h:panelGrid>
                        <br/><br/>
                        <div align="right">
                            <p:commandButton value="Get Shipping Address" oncomplete="caDialog.show();" update="caForm"/>
                        </div>
                        <br/>
                        <br/>
                    </h:panelGroup>
                    <h:panelGroup rendered="#{!sqManagerBean.salesQuotation.fullShipment}" >
                        <p:separator/>
                        <br/>
                        <p:fieldset legend="Delivery Order 1">
                            <br/>
                            <h:panelGrid columns="2">
                                <h:outputLabel value="Urgent: "/>
                                <h:selectBooleanCheckbox value="#{sqManagerBean.newDeliveryOrder.urgent}">
                                    <p:ajax update="salesQuotationForm"/>
                                </h:selectBooleanCheckbox>
                                <h:panelGroup rendered="#{sqManagerBean.newDeliveryOrder.urgent}">
                                    <h:outputLabel value="Requested Lead Time: " />
                                </h:panelGroup>
                                <h:panelGroup rendered="#{sqManagerBean.newDeliveryOrder.urgent}">
                                    <p:calendar value="#{sqManagerBean.date1}" onSelectUpdate="salesQuotationForm" showOn="button" />
                                </h:panelGroup>
                            </h:panelGrid>
                            <br/>
                            <h:panelGrid id="sa2" columns="2">
                                <h:outputLabel value="Address Line 1: "/>
                                <p:inputText disabled="true" size="80" value="#{sqManagerBean.newDeliveryOrder.shippingAddress.addressLine1}" label="Shipping Address Line 1" required="true" requiredMessage="Delivery Order 1 Shipping Address Line 1 required"/>

                                <h:outputLabel value="Address Line 2: "/>
                                <p:inputText disabled="true" size="80" value="#{sqManagerBean.newDeliveryOrder.shippingAddress.addressLine2}" label="Shipping Address Line 2" required="true" requiredMessage="Delivery Order 1 Shipping Address Line 2 required"/>

                                <h:outputLabel value="Zip Code: "/>
                                <p:inputMask disabled="true" mask="999999" value="#{sqManagerBean.newDeliveryOrder.shippingAddress.zipCode}" label="Zip Code" required="true" requiredMessage="Delivery Order 1 Shipping Address Zip Code required"/>

                                <h:outputLabel value="City: "/>  
                                <p:inputText disabled="true" size="20" value="#{sqManagerBean.newDeliveryOrder.shippingAddress.city}" label="City" required="true" requiredMessage="Delivery Order 1 Shipping Address City required"/>

                                <h:outputLabel value="State: "/>
                                <p:inputText disabled="true"  size="20" value="#{sqManagerBean.newDeliveryOrder.shippingAddress.stateName}" label="State" required="true" requiredMessage="Delivery Order 1 Shipping Address State required"/>

                                <h:outputLabel value="Country: "/>
                                <p:inputText disabled="true" size="20" value="#{sqManagerBean.newDeliveryOrder.shippingAddress.country}" label="Country" required="true" requiredMessage="Delivery Order 1 Shipping Address Country required"/>
                            </h:panelGrid>
                            <br/>
                            <h:panelGrid columns="3" cellpadding="10">
                                <h:outputLabel value="Indicative Lead Time: "/>
                                <h:outputText value="#{sqManagerBean.newDeliveryOrder.indicativeLeadTime}"/>
                                <p:commandButton value="Available to promise check" actionListener="#{sqManagerBean.aTPCheck1}" update="salesQuotationForm"/>
                            </h:panelGrid>
                            <br/><br/>
                            <div align="right">
                                <p:commandButton value="Get Shipping Address" oncomplete="caDialog.show();" update="caForm"/>
                            </div>
                            <br/><br/>
                            <p:dataTable var="oneIL" rowIndexVar="rowIndex" value="#{sqManagerBean.newDeliveryOrder.itemLines}">
                                <p:column headerText="Product Type" rendered="#{sqManagerBean.displayItemLine(rowIndex)}">
                                    <h:outputText value="#{oneIL.product.productType}"/>
                                </p:column>
                                <p:column headerText="Quantity Ordered Case" rendered="#{sqManagerBean.displayItemLine(rowIndex)}">
                                    <h:inputText label="Order Quantity" required="true" requiredMessage="Please enter order quantity" value="#{oneIL.qtyOrderedCase}"/>
                                </p:column>
                            </p:dataTable>
                            <br/>
                        </p:fieldset>
                        <br/>
                        <br/>
                        <p:fieldset legend="Delivery Order 2">
                            <br/>
                            <h:panelGrid columns="2">
                                <h:outputLabel value="Urgent: "/>
                                <h:selectBooleanCheckbox value="#{sqManagerBean.newDeliveryOrder2.urgent}">
                                    <p:ajax update="salesQuotationForm"/>
                                </h:selectBooleanCheckbox>
                                <h:panelGroup rendered="#{sqManagerBean.newDeliveryOrder2.urgent}">
                                    <h:outputLabel value="Requested Lead Time: " />
                                </h:panelGroup>
                                <h:panelGroup rendered="#{sqManagerBean.newDeliveryOrder2.urgent}">
                                    <p:calendar value="#{sqManagerBean.date2}" onSelectUpdate="salesQuotationForm" showOn="button" />
                                </h:panelGroup>
                            </h:panelGrid>
                            <br/>

                            <h:panelGrid id="sa3" columns="2">
                                <h:outputLabel value="Address Line 1: "/>
                                <p:inputText disabled="true" size="80" value="#{sqManagerBean.newDeliveryOrder2.shippingAddress.addressLine1}" label="Shipping Address Line 1" required="true" requiredMessage="Delivery Order 2 Shipping Address Line 1 required"/>

                                <h:outputLabel value="Address Line 2: "/>
                                <p:inputText disabled="true" size="80" value="#{sqManagerBean.newDeliveryOrder2.shippingAddress.addressLine2}" label="Shipping Address Line 2" required="true" requiredMessage="Delivery Order 2 Shipping Address Line 2 required"/>

                                <h:outputLabel value="Zip Code: "/>
                                <p:inputMask disabled="true" mask="999999" value="#{sqManagerBean.newDeliveryOrder2.shippingAddress.zipCode}" label="Zip Code" required="true" requiredMessage="Delivery Order 2 Shipping Address Zip Code required"/>

                                <h:outputLabel value="City: "/>  
                                <p:inputText disabled="true" size="20" value="#{sqManagerBean.newDeliveryOrder2.shippingAddress.city}" label="City" required="true" requiredMessage="Delivery Order 2 Shipping Address City required"/>

                                <h:outputLabel value="State: "/>
                                <p:inputText disabled="true"  size="20" value="#{sqManagerBean.newDeliveryOrder2.shippingAddress.stateName}" label="State" required="true" requiredMessage="Delivery Order 2 Shipping Address State required"/>

                                <h:outputLabel value="Country: "/>
                                <p:inputText disabled="true" size="20" value="#{sqManagerBean.newDeliveryOrder2.shippingAddress.country}" label="Country" required="true" requiredMessage="Delivery Order 2 Shipping Address Country required"/>
                            </h:panelGrid>
                            <br/>
                            <h:panelGrid columns="3" cellpadding="10">
                                <h:outputLabel value="Indicative Lead Time: "/>
                                <h:outputText value="#{sqManagerBean.newDeliveryOrder2.indicativeLeadTime}"/>
                                <p:commandButton value="Available to promise check" actionListener="#{sqManagerBean.aTPCheck2}" update="salesQuotationForm"/>
                            </h:panelGrid>
                            <br/><br/>
                            <div align="right">
                                <p:commandButton value="Get Shipping Address" oncomplete="caDialog2.show();" update="caForm"/>
                            </div>
                            <br/><br/>
                            <p:dataTable var="oneIL" rowIndexVar="rowIndex" value="#{sqManagerBean.newDeliveryOrder2.itemLines}">
                                <p:column headerText="Product Type" rendered="#{sqManagerBean.displayItemLine(rowIndex)}">
                                    <h:outputText value="#{oneIL.product.productType}" />
                                </p:column>
                                <p:column headerText="Quantity Ordered Case" rendered="#{sqManagerBean.displayItemLine(rowIndex)}">
                                    <h:inputText label="Order Quantity" required="true" requiredMessage="Please enter order quantity" value="#{oneIL.qtyOrderedCase}"/>
                                </p:column>
                            </p:dataTable>
                            <br/>
                        </p:fieldset>
                        <br/>
                    </h:panelGroup>
                </h:panelGroup>
                <br/>
            </p:panel>
            <br/>
            <br/>
            <div align="right">
                <p:commandButton value="Next" ajax="false" action="#{sqManagerBean.recalculateSalesQuotation}"/>
            </div>
            <br/>
            <br/>

        </h:form>

        <p:dialog header="Search Client Address" widgetVar="caDialog" showEffect="fade" hideEffect="fade" height="600" width="800" resizable="false">
            <h:form id="caForm">           
                <br/>
                <p:dataTable var="ca" value="#{sqManagerBean.clientAddress}" emptyMessage="No address found"  selection="#{sqManagerBean.newDeliveryOrder.shippingAddress}" selectionMode="single">

                    <p:column filterBy="#{ca.addressLine1}" 
                              headerText="Address Line 1" filterMatchMode="contains">
                        <h:outputText value="#{ca.addressLine1}" />
                    </p:column>

                    <p:column filterBy="#{ca.addressLine2}" 
                              headerText="Address Line 2" filterMatchMode="contains">
                        <h:outputText value="#{ca.addressLine2}" />
                    </p:column>

                </p:dataTable>
                <br/>
                <div align="right">
                    <p:commandButton value="Add Shipping Address" update="caForm caForm2" oncomplete="addSADialog.show()"/>
                    <p:commandButton value="Select" update="salesQuotationForm:sa1 salesQuotationForm:sa2" oncomplete="caDialog.hide()"/>
                </div>
            </h:form>
        </p:dialog>

        <p:dialog header="Search Client Address" widgetVar="caDialog2" showEffect="fade" hideEffect="fade" height="600" width="800" resizable="false">
            <h:form id="caForm2">           
                <br/>
                <p:dataTable var="ca" value="#{sqManagerBean.clientAddress}" emptyMessage="No address found"  selection="#{sqManagerBean.newDeliveryOrder2.shippingAddress}" selectionMode="single">

                    <p:column filterBy="#{ca.addressLine1}" 
                              headerText="Address Line 1" filterMatchMode="contains">
                        <h:outputText value="#{ca.addressLine1}" />
                    </p:column>

                    <p:column filterBy="#{ca.addressLine2}" 
                              headerText="Address Line 2" filterMatchMode="contains">
                        <h:outputText value="#{ca.addressLine2}" />
                    </p:column>

                </p:dataTable>
                <br/>
                <div align="right">
                    <p:commandButton value="Add Shipping Address" update="caForm caForm2" oncomplete="addSADialog.show()"/>
                    <p:commandButton value="Select" update="salesQuotationForm:sa3" oncomplete="caDialog2.hide()"/>
                </div>
            </h:form>
        </p:dialog>

        <p:dialog id="addShippingAddressDialog" header="Add Shipping Address" widgetVar="addSADialog" modal="true" showEffect="fade" hideEffect="fade" resizable="false" width="800" closable="false">
            <h:form id="addSAForm">
                <p:messages showDetail="true"/>
                <h:panelGrid columns="2" style="border-color: transparent" width="100%">

                    <h:outputLabel value="Address Line 1: "/>
                    <p:inputText size="80" value="#{sqManagerBean.newAddress.addressLine1}" label="Shipping Address Line 1"/>

                    <h:outputLabel value="Address Line 2: "/>
                    <p:inputText size="80" value="#{sqManagerBean.newAddress.addressLine2}" label="Shipping Address Line 2"/>

                    <h:outputLabel value="Zip Code: "/>
                    <p:inputMask size="6" value="#{sqManagerBean.newAddress.zipCode}" mask="999999" label="Zip Code"/>

                    <h:outputLabel value="City: "/>  
                    <p:inputText size="20" value="#{sqManagerBean.newAddress.city}" label="City"/>

                    <h:outputLabel value="State: "/>
                    <p:inputText size="20" value="#{sqManagerBean.newAddress.stateName}" label="State"/>

                    <h:outputLabel value="Country: "/>
                    <p:inputText size="20" value="#{sqManagerBean.newAddress.country}" label="Country"/>

                </h:panelGrid>
                <br/>
                <div align="right">
                    <p:commandButton value="Add" actionListener="#{sqManagerBean.addShippingAddress()}" update="salesQuotationForm addSAForm caForm caForm2" oncomplete="handleAddShippingAddress(xhr, status, args)" image="ui-icon ui-icon-check"/>
                    <p:commandButton value="Close" oncomplete="addSADialog.hide()" image="ui-icon ui-icon-closethick"/> 
                </div>
            </h:form>
        </p:dialog>

        <script type="text/javascript">  
            function handleAddShippingAddress(xhr, status, args) {
                if(!args.valid) {  
                    jQuery('#addShippingAddressDialog').parent().effect("shake", { times:3 }, 100);
                } else {
                    addSADialog.hide();
                }  
            }  
        </script>

    </ui:define>

    <ui:define name="bottom">

    </ui:define>

</ui:composition>



